using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using BitMango.Diagnostics;

namespace BitMango.MultiPlayer
{
    public static class Match
    {
        //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static event Action<object> OnSend;

        //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static event Action<object> OnRecv;

        //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static event Action<object> OnFin;

        //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static event Action<bool> OnClose;

        public static bool IsMaster
        {
            get
            {
                return Match.isMaster;
            }
            set
            {
                Match.isMaster = value;
            }
        }

        public static string TypeName
        {
            get
            {
                return Match.impl.GetType().FullName;
            }
        }

        public static void SetActive(MatchImplBase _impl)
        {
            BitMango.Diagnostics.Debugger.Log("Match.SetActive : " + _impl.GetType().FullName);
            if (Match.impl != null)
            {
                MatchImplBase matchImplBase = Match.impl;
                Delegate onRecv = matchImplBase.OnRecv;
                if (Match._003C_003Ef__mg_0024cache0 == null)
                {
                    Match._003C_003Ef__mg_0024cache0 = new Action<object>(Match.FireOnRecv);
                }
                matchImplBase.OnRecv = (Action<object>)Delegate.Remove(onRecv, Match._003C_003Ef__mg_0024cache0);
                MatchImplBase matchImplBase2 = Match.impl;
                Delegate onFin = matchImplBase2.OnFin;
                if (Match._003C_003Ef__mg_0024cache1 == null)
                {
                    Match._003C_003Ef__mg_0024cache1 = new Action<object>(Match.FireOnFin);
                }
                matchImplBase2.OnFin = (Action<object>)Delegate.Remove(onFin, Match._003C_003Ef__mg_0024cache1);
                MatchImplBase matchImplBase3 = Match.impl;
                Delegate onClose = matchImplBase3.OnClose;
                if (Match._003C_003Ef__mg_0024cache2 == null)
                {
                    Match._003C_003Ef__mg_0024cache2 = new Action<bool>(Match.FireOnClose);
                }
                matchImplBase3.OnClose = (Action<bool>)Delegate.Remove(onClose, Match._003C_003Ef__mg_0024cache2);
            }
            Match.impl = _impl;
            MatchImplBase matchImplBase4 = Match.impl;
            Delegate onRecv2 = matchImplBase4.OnRecv;
            if (Match._003C_003Ef__mg_0024cache3 == null)
            {
                Match._003C_003Ef__mg_0024cache3 = new Action<object>(Match.FireOnRecv);
            }
            matchImplBase4.OnRecv = (Action<object>)Delegate.Combine(onRecv2, Match._003C_003Ef__mg_0024cache3);
            MatchImplBase matchImplBase5 = Match.impl;
            Delegate onFin2 = matchImplBase5.OnFin;
            if (Match._003C_003Ef__mg_0024cache4 == null)
            {
                Match._003C_003Ef__mg_0024cache4 = new Action<object>(Match.FireOnFin);
            }
            matchImplBase5.OnFin = (Action<object>)Delegate.Combine(onFin2, Match._003C_003Ef__mg_0024cache4);
            MatchImplBase matchImplBase6 = Match.impl;
            Delegate onClose2 = matchImplBase6.OnClose;
            if (Match._003C_003Ef__mg_0024cache5 == null)
            {
                Match._003C_003Ef__mg_0024cache5 = new Action<bool>(Match.FireOnClose);
            }
            matchImplBase6.OnClose = (Action<bool>)Delegate.Combine(onClose2, Match._003C_003Ef__mg_0024cache5);
        }

        public static void InitUser()
        {
            BitMango.Diagnostics.Debugger.Assert(Match.Me != null, "Match101.InitUser :: Me. is null");
            BitMango.Diagnostics.Debugger.Assert(Match.Opponent != null, "Match101.InitUser :: Me. is null");
            Match.Me.ready = false;
            Match.Me.rematchAnswer = 0;
            Match.Opponent.ready = false;
            Match.Opponent.rematchAnswer = 0;
        }

        public static bool IsOpen
        {
            get
            {
                return Match.impl.IsOpen;
            }
        }

        public static void Open(int _gameVariant, Action<bool> callback)
        {
            Match.gameVariant = _gameVariant;
            Match.impl.Open(_gameVariant, callback);
        }

        public static void Ready(object userData, object pkt, Action<Match.User, object> callback)
        {
            Match.impl.Ready(userData, pkt, callback);
        }

        public static void Send(object msg)
        {
            Match.impl.Send(msg);
            Match.FireOnSend(msg);
        }

        public static void ReMatch(bool answer, Action<bool> callback)
        {
            Match.impl.ReMatch(answer, callback);
        }

        public static void Close(bool opponent)
        {
            Match.impl.Close(opponent);
        }

        public static void Fin(object pkt)
        {
            Match.impl.Fin(pkt);
        }

        private static void FireOnSend(object msg)
        {
            Match.OnSend(msg);
        }

        private static void FireOnRecv(object msg)
        {
            if (Match.OnRecv != null)
            {
                Match.OnRecv(msg);
            }
        }

        private static void FireOnFin(object msg)
        {
            if (Match.OnFin != null)
            {
                Match.OnFin(msg);
            }
        }

        private static void FireOnClose(bool opponent)
        {
            if (Match.OnClose != null)
            {
                Match.OnClose(opponent);
            }
        }

        public static Match.User Me;

        public static Match.User Opponent;

        private static bool isMaster;

        public static int gameVariant;

        private static MatchImplBase impl;

        [CompilerGenerated]
        private static Action<object> _003C_003Ef__mg_0024cache0;

        [CompilerGenerated]
        private static Action<object> _003C_003Ef__mg_0024cache1;

        [CompilerGenerated]
        private static Action<bool> _003C_003Ef__mg_0024cache2;

        [CompilerGenerated]
        private static Action<object> _003C_003Ef__mg_0024cache3;

        [CompilerGenerated]
        private static Action<object> _003C_003Ef__mg_0024cache4;

        [CompilerGenerated]
        private static Action<bool> _003C_003Ef__mg_0024cache5;

        [Serializable]
        public class User
        {
            public object userData;

            public bool ready;

            public int rematchAnswer;
        }
    }
}
